var $ = require('../../common/helper.js');
Page({
	data: {
		casy: [],
		row: {},
		casy_id: 0,
		money: 0,
		casy_name: '',
		gjj_money: '',
		gjj_money_min: 0,
		gjj_money_max: 0,
		gjj_btn: 'btn-x'
	},
	onLoad: function (options) {
		if (typeof (options.city_id) == 'undefined' || !options.city_id.length) {
			$.overloadError('请先选择参保地');
			wx.navigateBack();
			return;
		}
		var _this = this;
		$.get('api.php?app=cart&act=casy&city_id=' + options.city_id, function (json) {
			_this.setData({
				casy: json.data.casy,
				row: json.data.row,
				casy_id: json.data.casy[0].id,
				money: Math.floor(json.data.casy[0].money),
				casy_name: json.data.casy[0].name,
				gjj_money: options.gjj,
				gjj_money_min: Math.floor(json.data.row.money),
				gjj_money_max: Math.floor(json.data.row.max_money),
				gjj_btn: (!options.gjj || Number(options.gjj) != Number(json.data.row.money)) ? '' : 'btn-x'
			});
		});
	},
	change: function (e) {
		var index = Number(e.detail.value);
		this.setData({
			casy_id: this.data.casy[index].id,
			money: this.data.casy[index].money,
			casy_name: this.data.casy[index].name
		});
	},
	toggleGjj: function () {
		var x = this.data.gjj_btn == 'btn-x';
		this.setData({
			gjj_money: x ? '' : this.data.gjj_money_min,
			gjj_btn: x ? '' : 'btn-x'
		});
	},
	changeGjj: function (e) {
		var money = Number(e.detail.value), btn = '';
		if (money == Number(this.data.gjj_money_min)) btn = 'btn-x';
		this.setData({
			gjj_money: e.detail.value,
			gjj_btn: btn
		});
	},
	submit: function () {
		var money = this.data.gjj_money;
		if (money.length && (Number(money) < this.data.gjj_money_min || Number(money) > this.data.gjj_money_max)) {
			$.overloadError('公积金范围值为' + parseInt(this.data.gjj_money_min) + '~' + this.data.gjj_money_max);
			return;
		}
		var apps = $.getApps(), app = apps[apps.length - 2],
			data = {
				casy_id: this.data.casy_id,
				name: this.data.casy_name,
				gjj: money
			};
		app.setParam('casy', data);
		wx.navigateBack();
	}
})